package cn.stylefeng.guns.modular.business.frequency.transportreserverecord.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.transportreserverecord.entity.TransportReserveRecord;
import cn.stylefeng.guns.modular.business.frequency.transportreserverecord.mapper.TransportReserveRecordMapper;
import cn.stylefeng.guns.modular.business.frequency.transportreserverecord.model.param.TransportReserveRecordParam;
import cn.stylefeng.guns.modular.business.frequency.transportreserverecord.service.TransportReserveRecordService;
import cn.stylefeng.guns.modular.business.frequency.transportreserverecord.enums.TransportReserveRecordExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 运单签收是否装车记录表 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:15
 */
@Service
public class TransportReserveRecordServiceImpl extends ServiceImpl<TransportReserveRecordMapper, TransportReserveRecord> implements TransportReserveRecordService {

    @Override
    public PageResult<TransportReserveRecord> page(TransportReserveRecordParam transportReserveRecordParam) {

        // 构造条件
        LambdaQueryWrapper<TransportReserveRecord> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<TransportReserveRecord> list(TransportReserveRecordParam transportReserveRecordParam) {

        // 构造条件
        LambdaQueryWrapper<TransportReserveRecord> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(TransportReserveRecordParam transportReserveRecordParam) {

        // 将dto转为实体
        TransportReserveRecord transportReserveRecord = new TransportReserveRecord();
        BeanUtil.copyProperties(transportReserveRecordParam, transportReserveRecord);

        this.save(transportReserveRecord);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(TransportReserveRecordParam transportReserveRecordParam) {
        this.removeById(transportReserveRecordParam.getId());
    }

    @Override
    public void edit(TransportReserveRecordParam transportReserveRecordParam) {

        // 根据id查询实体
        TransportReserveRecord transportReserveRecord = this.queryTransportReserveRecord(transportReserveRecordParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(transportReserveRecordParam, transportReserveRecord);

        this.updateById(transportReserveRecord);
    }

    @Override
    public TransportReserveRecord detail(TransportReserveRecordParam transportReserveRecordParam) {
        return this.queryTransportReserveRecord(transportReserveRecordParam);
    }



    /**
     * 获取运单签收是否装车记录表
     *
     * @author yujie
     * @since 2023/05/09 17:15
     */
    private TransportReserveRecord queryTransportReserveRecord(TransportReserveRecordParam transportReserveRecordParam) {
        TransportReserveRecord transportReserveRecord = this.getById(transportReserveRecordParam.getId());
        if (ObjectUtil.isEmpty(transportReserveRecord)) {
            throw new ServiceException(TransportReserveRecordExceptionEnum.NOT_EXISTED);
        }
        return transportReserveRecord;
    }
}